/////////////////////////////////////////////////////////////////////////////
// Name:        dictpackpanel.cpp
// Purpose:     
// Author:      Brant Young
// Modified by: 
// Created:     Sat Jun 23 14:55:29 2007
// RCS-ID:      
// Copyright:   (C) Victsoft 2007
// Licence:     GPL 2.0
/////////////////////////////////////////////////////////////////////////////

#include <wx/wx.h>
#include <wx/event.h>
#include <wx/fs_mem.h>

#include "../component/chmhtmlwnd.h"
#include "dictpackpanel.h"
#include "../../core/dictmgr.h"
#include "../../core/dictquery.h"
#include "../../core/thememgr.h"

DictPackPanel::DictPackPanel (wxWindow* p_parent) : wxPanel(p_parent, ID_DICTPACK_PANEL, wxDefaultPosition, wxDefaultSize, wxSUNKEN_BORDER|wxTAB_TRAVERSAL)
{
	_p_thememgr = &ThemeManager::get();
	_p_dictmgr = &DictManager::get();
	
	////@begin DictPackPanel content construction
    p_root_sizer = new wxBoxSizer(wxHORIZONTAL);
    this->SetSizer(p_root_sizer);

    // p_dict_sizer = new wxBoxSizer(wxVERTICAL);
    //wxSplitterWindow* p_spltwnd = new wxSplitterWindow(this, \
    //        wxID_ANY, \
    //        wxDefaultPosition, wxSize(450, 400), \
    //        wxNO_BORDER|wxTAB_TRAVERSAL);
    
    //p_root_sizer->Add(p_spltwnd, 1, wxGROW, 0);

    p_dict_htmllist = new wxSimpleHtmlListBox(this, ID_DICT_HTMLLIST, wxDefaultPosition, wxSize(400,300), 0, NULL, wxHLB_MULTIPLE);
    // p_dict_htmllist->SetSelectionBackground(wxColor(_T("#FFD79C")));
    p_root_sizer->Add(p_dict_htmllist, 1, wxGROW, 0);
    //p_dict_sizer->Layout();

    // p_info_htmlwnd = new CHMHtmlWindow(p_spltwnd, ID_INFO_HTMLWND, wxDefaultPosition, wxDefaultSize, wxSUNKEN_BORDER);
    //p_dict_sizer->Add(p_info_htmlwnd, 0, wxGROW, 0);
    

    p_button_sizer = new wxBoxSizer(wxVERTICAL);
    p_root_sizer->Add(p_button_sizer, 0, wxGROW|wxALL, 5);

    p_disable_btn = new wxButton( this, ID_DISABLE_BTN, _("&Disable"), wxDefaultPosition, wxDefaultSize, 0 );
    p_button_sizer->Add(p_disable_btn, 0, wxGROW|wxLEFT|wxRIGHT|wxTOP, 10);

    p_uninstall_btn = new wxButton( this, ID_UNINSTALL_BTN, _("&Uninstall"), wxDefaultPosition, wxDefaultSize, 0 );
    p_button_sizer->Add(p_uninstall_btn, 0, wxGROW|wxLEFT|wxRIGHT|wxTOP, 10);
    
    p_stcline = new wxStaticLine( this, wxID_STATIC, wxDefaultPosition, wxDefaultSize, wxLI_HORIZONTAL );
    p_button_sizer->Add(p_stcline, 0, wxGROW|wxALL, 10);

    p_property_btn = new wxButton( this, ID_PROPERTY_BTN, _("&Property"), wxDefaultPosition, wxDefaultSize, 0 );
    p_button_sizer->Add(p_property_btn, 0, wxGROW|wxALL, 10);

    wxBitmap& top_bmp = _p_thememgr->get_bitmap(_T("go-top-24.png"));
    p_top_btn = new wxBitmapButton( this, ID_TOP_BTN, top_bmp);
    p_top_btn->SetToolTip(_("Move Top"));
    p_button_sizer->Add(p_top_btn, 0, wxGROW|wxLEFT|wxRIGHT|wxBOTTOM, 10);

    wxBitmap& up_bmp = _p_thememgr->get_bitmap(_T("go-up-24.png"));
    p_up_btn = new wxBitmapButton( this, ID_UP_BTN, up_bmp);
    p_up_btn->SetToolTip(_("Move Up"));
    p_button_sizer->Add(p_up_btn, 0, wxGROW|wxLEFT|wxRIGHT|wxBOTTOM, 10);

    wxBitmap& down_bmp = _p_thememgr->get_bitmap(_T("go-down-24.png"));
    p_down_btn = new wxBitmapButton( this, ID_DOWN_BTN, down_bmp);
    p_down_btn->SetToolTip(_("Move Down"));
    p_button_sizer->Add(p_down_btn, 0, wxGROW|wxLEFT|wxRIGHT|wxBOTTOM, 10);

    wxBitmap& bottom_bmp = _p_thememgr->get_bitmap(_T("go-bottom-24.png"));
    p_bottom_btn = new wxBitmapButton( this, ID_BOTTOM_BTN, bottom_bmp);
    p_bottom_btn->SetToolTip(_("Move Bottom"));
    p_button_sizer->Add(p_bottom_btn, 0, wxGROW|wxLEFT|wxRIGHT|wxBOTTOM, 10);
    
    wxBitmap& bmp = _p_thememgr->get_bitmap(_T("book-close-32.png"));
    wxMemoryFSHandler::AddFile(_T("book-close-32.png"), bmp, wxBITMAP_TYPE_PNG);
    bmp = _p_thememgr->get_bitmap(_T("book-close-disable-32.png"));
    wxMemoryFSHandler::AddFile(_T("book-close-disable-32.png"), bmp, wxBITMAP_TYPE_PNG);
    
	////@end DictPackPanel content construction
    /*p_spltwnd->Initialize(p_dict_htmllist);
    p_spltwnd->Initialize(p_info_htmlwnd);
    p_spltwnd->SplitHorizontally(p_dict_htmllist,p_info_htmlwnd);
    p_spltwnd->SetSashGravity(1.0);
    p_spltwnd->SetMinimumPaneSize(2);
    p_spltwnd->SetSashPosition(200);*/
    
    Fit();
    InitEvents();
    _init_dicts();
}


void DictPackPanel::_init_dicts() {	
	p_dict_htmllist->Clear();
	
	for (int i=0; i<_p_dictmgr->installed_dict_count; i++) {
		p_dict_htmllist->Append(_(""));
		_set_dict_item_color(i);
	}
}


void DictPackPanel::InitEvents() {
	this->Connect(ID_DICT_HTMLLIST, wxEVT_COMMAND_LISTBOX_SELECTED, 
			(wxObjectEventFunction)&DictPackPanel::OnDictSelected);
	this->Connect(ID_DICT_HTMLLIST, wxEVT_COMMAND_LISTBOX_DOUBLECLICKED, 
		(wxObjectEventFunction)&DictPackPanel::OnDictProperty);
	
	this->Connect(ID_DISABLE_BTN, wxEVT_COMMAND_BUTTON_CLICKED, 
			(wxObjectEventFunction)&DictPackPanel::OnDisableDict);
	this->Connect(ID_UNINSTALL_BTN, wxEVT_COMMAND_BUTTON_CLICKED, 
			(wxObjectEventFunction)&DictPackPanel::OnUninstallDict);
	this->Connect(ID_TOP_BTN, wxEVT_COMMAND_BUTTON_CLICKED, 
			(wxObjectEventFunction)&DictPackPanel::OnMoveTop);
	this->Connect(ID_UP_BTN, wxEVT_COMMAND_BUTTON_CLICKED, 
			(wxObjectEventFunction)&DictPackPanel::OnMoveUp);
	this->Connect(ID_DOWN_BTN, wxEVT_COMMAND_BUTTON_CLICKED, 
			(wxObjectEventFunction)&DictPackPanel::OnMoveDown);
	this->Connect(ID_BOTTOM_BTN, wxEVT_COMMAND_BUTTON_CLICKED, 
			(wxObjectEventFunction)&DictPackPanel::OnMoveBottom);
	this->Connect(ID_PROPERTY_BTN, wxEVT_COMMAND_BUTTON_CLICKED, 
			(wxObjectEventFunction)&DictPackPanel::OnDictProperty);
}


void DictPackPanel::OnMoveTop(wxCommandEvent& event) {	
	int index = p_dict_htmllist->GetSelection();
	if (wxNOT_FOUND==index) {
		return;
	}

	_p_dictmgr->swap_dicts_order(_p_dictmgr->installed_dict_filenames[index], index, \
			_p_dictmgr->installed_dict_filenames[0], 0);
    _p_dictmgr->reinit_installed_dict_info();
	
	for (int i=0; i<=index; i++) {
		_set_dict_item_color(i);
	}

	p_dict_htmllist->Toggle(index);
	p_dict_htmllist->SetSelection(0);
	OnDictSelected(event);
}


void DictPackPanel::OnMoveUp(wxCommandEvent& event) {	
	int index = p_dict_htmllist->GetSelection();
	if (wxNOT_FOUND==index) {
		return;
	}

	_p_dictmgr->swap_dicts_order(_p_dictmgr->installed_dict_filenames[index], index, \
			_p_dictmgr->installed_dict_filenames[index-1], index-1);
    _p_dictmgr->reinit_installed_dict_info();
	
	_set_dict_item_color(index);
	_set_dict_item_color(index-1);

	p_dict_htmllist->Toggle(index);
	p_dict_htmllist->SetSelection(index-1);
	OnDictSelected(event);
}


void DictPackPanel::OnMoveDown(wxCommandEvent& event) {	
	int index = p_dict_htmllist->GetSelection();
	if (wxNOT_FOUND==index) {
		return;
	}

	_p_dictmgr->swap_dicts_order(_p_dictmgr->installed_dict_filenames[index], index, \
			_p_dictmgr->installed_dict_filenames[index+1], index+1);
    _p_dictmgr->reinit_installed_dict_info();

	_set_dict_item_color(index);
	_set_dict_item_color(index+1);

	p_dict_htmllist->Toggle(index);
	p_dict_htmllist->SetSelection(index+1);
	OnDictSelected(event);
}


void DictPackPanel::OnMoveBottom(wxCommandEvent& event) {	
	int index = p_dict_htmllist->GetSelection();
	if (wxNOT_FOUND==index) {
		return;
	}
	
	int count = p_dict_htmllist->GetCount();
	_p_dictmgr->swap_dicts_order(_p_dictmgr->installed_dict_filenames[index], index, \
			_p_dictmgr->installed_dict_filenames[count-1], count-1);
    _p_dictmgr->reinit_installed_dict_info();

	for (int i=index; i<count; i++) {
		_set_dict_item_color(i);
	}
	
    wxCommandEvent evt(wxEVT_COMMAND_LISTBOX_SELECTED, ID_DICT_HTMLLIST);
    evt.SetEventObject(this);
    
    /*evt.SetInt(index);
	evt.SetExtraLong(false);
	ProcessEvent(evt);
    
    evt.SetInt(count-1);
    evt.SetExtraLong(true);
	ProcessEvent(evt);*/

	p_dict_htmllist->Toggle(index);
	p_dict_htmllist->SetSelection(count-1);
	OnDictSelected(event);
}


void DictPackPanel::OnDisableDict(wxCommandEvent& event) {	
	int index = p_dict_htmllist->GetSelection();
	if (wxNOT_FOUND==index) {
		return;
	}
	
	if (1==_p_dictmgr->installed_dict_status[index]) { // enabled, then disable it
		_p_dictmgr->disable_dict(_p_dictmgr->installed_dict_filenames[index]);
	    _p_dictmgr->reinit_installed_dict_info();
		p_disable_btn->Enable();
		p_disable_btn->SetLabel(_("&Enable"));
	} else if (-1==_p_dictmgr->installed_dict_status[index]) {
		_p_dictmgr->enable_dict(_p_dictmgr->installed_dict_filenames[index]);
	    _p_dictmgr->reinit_installed_dict_info();
		p_disable_btn->Enable();
		p_disable_btn->SetLabel(_("&Disable"));
	} else {
		p_disable_btn->Disable();
	}
	
	_set_dict_item_color(index);
}


void DictPackPanel::OnDictSelected(wxCommandEvent& event) {	
	int index = p_dict_htmllist->GetSelection();
	if (wxNOT_FOUND==index) {
		return;
	}
	
	if (p_dict_htmllist->GetSelectedCount()>1) {
	    p_top_btn->Disable();
	    p_bottom_btn->Disable();
	    p_up_btn->Disable();
	    p_down_btn->Disable();
	    p_disable_btn->Disable();
	    return;
	}

    p_disable_btn->Enable();
	if (0==index) { // topmost dict
	    p_top_btn->Disable();
	    p_up_btn->Disable();
	    p_bottom_btn->Enable();
	    p_down_btn->Enable();
	} else if ((_p_dictmgr->installed_dict_count-1)==index) { // bottom dict
	    p_top_btn->Enable();
	    p_up_btn->Enable();
	    p_bottom_btn->Disable();
	    p_down_btn->Disable();
	}
	
	if (1==_p_dictmgr->installed_dict_status[index]) {
		p_disable_btn->Enable();
		p_disable_btn->SetLabel(_("&Disable"));
	} else if (-1==_p_dictmgr->installed_dict_status[index]) {
		p_disable_btn->Enable();
		p_disable_btn->SetLabel(_("&Enable"));
	} else {
		p_disable_btn->Disable();
	}
}


void DictPackPanel::OnUninstallDict(wxCommandEvent& event) {	
	int index = p_dict_htmllist->GetSelection();
	if (wxNOT_FOUND==index) {
		return;
	}

	_p_dictmgr->delete_all_dicts_dbconn();
	
	unsigned long cookie;
	index = p_dict_htmllist->GetFirstSelected(cookie);
	while (index != wxNOT_FOUND) { 
	    _p_dictmgr->uninstall_dict(_p_dictmgr->installed_dict_filenames[index]);
	    _p_dictmgr->reinit_installed_dict_info();
		p_dict_htmllist->Delete(index);
		
	    index = p_dict_htmllist->GetNextSelected(cookie);
	}
	
	_p_dictmgr->create_all_dicts_dbconn();
}


void DictPackPanel::OnDictProperty(wxCommandEvent& event) {
	int index = p_dict_htmllist->GetSelection();
	wxDialog dictdlg;

	dictdlg.SetExtraStyle(wxWS_EX_BLOCK_EVENTS);
	dictdlg.Create(this, wxID_ANY, _("Add-ons"), wxDefaultPosition, \
    		wxDefaultSize, wxCAPTION| \
    		wxSYSTEM_MENU|wxCLOSE_BOX);

	wxBoxSizer* p_sizer = new wxBoxSizer(wxHORIZONTAL);
	dictdlg.SetSizer(p_sizer);
    
	CHMHtmlWindow* p_info_htmlwnd = new CHMHtmlWindow(&dictdlg, wxID_ANY, wxDefaultPosition, wxSize(400,400), wxSUNKEN_BORDER);
	
	wxString& dictfn = _p_dictmgr->installed_dict_filenames[index];
	
	_p_dictmgr->dict_dir.SetFullName(dictfn);
    DictQuery dictq(_p_dictmgr->dict_dir.GetFullPath());

    // wxMessageBox(dictq.name.c_str());
	wxString html;
	html.Printf(_T("<body> \
			<table border=\"0\" cellpadding=\"2\" cellspacing=\"2\" \
			 width=\"100%\"> \
			    <tr bgcolor=\"#D3E7FF\"> \
			      <td width=\"60\" valign=\"top\"><b>Name: </b></td> \
			      <td>%s</td> \
			    </tr> \
			    <tr bgcolor=\"#E5F0FF\"> \
			      <td valign=\"top\"><b>Category: </b></td> \
			      <td>%s</td> \
			    </tr> \
			    <tr bgcolor=\"#D3E7FF\"> \
			      <td valign=\"top\"><b>Description: </b></td> \
			      <td>%s</td> \
			    </tr> \
			    <tr bgcolor=\"#E5F0FF\"> \
			      <td valign=\"top\"><b>Version: </b></td> \
			      <td>%s</td> \
			    </tr> \
			    <tr bgcolor=\"#D3E7FF\"> \
			      <td valign=\"top\"><b>File: </b></td> \
			      <td>%s</td> \
			    </tr> \
			    <tr bgcolor=\"#E5F0FF\"> \
			      <td valign=\"top\"><b>Author: </b></td> \
			      <td>%s</td> \
			    </tr> \
			    <tr bgcolor=\"#D3E7FF\"> \
			      <td valign=\"top\"><b>Date: </b></td> \
			      <td>%s</td> \
			    </tr> \
			    <tr bgcolor=\"#E5F0FF\"> \
			      <td valign=\"top\"><b>Email: </b></td> \
			      <td>%s</td> \
			    </tr> \
			    <tr bgcolor=\"#D3E7FF\"> \
			      <td valign=\"top\"><b>Website: </b></td> \
			      <td>%s</td> \
			    </tr> \
			</table> \
			</body> \
		"), dictq.name.c_str(), _(""), \
			dictq.description.c_str(), dictq.version.c_str(), \
			dictfn.c_str(), dictq.author.c_str(), dictq.date.c_str(), \
			dictq.email.c_str(), dictq.website.c_str()
	);
	//wxMessageBox(html);
	
	p_info_htmlwnd->SetPage(html);
	p_sizer->Add(p_info_htmlwnd, 1, wxGROW, 0);
	
	dictdlg.ShowModal();
}


bool DictPackPanel::_set_dict_item_color(int index) {
	wxString fontcolor(_T("#000000"));
	wxString image_str(_T("memory:book-close-32.png"));
	wxString html;
	
	//if (1==_p_dictmgr->installed_dict_status[index]){
		//fontcolor = _T("#CE8642");
	//} else 
	if (-1==_p_dictmgr->installed_dict_status[index]){
		fontcolor = _T("#919092");
		image_str = _T("memory:book-close-disable-32.png");
	}
	
	/*"<table border="0" cellpadding="2" cellspacing="2"
 width="100%">
    <tr>
      <td width="32"> <img src=\"memory:book-close-32.png\" /> </td>
      <td> </<b><font color=\"%s\">%s</font></b> </td>
    </tr>
</table>
"*/
	html.Printf(_T("<table border=\"0\" cellpadding=\"2\" cellspacing=\"2\" \
 width=\"400\"> \
    <tr> \
      <td width=\"32\"> <img src=\"%s\" /> </td> \
      <td> <b><font color=\"%s\">%s</font></b> </td> \
    </tr> \
</table> \
"), image_str.c_str(), fontcolor.c_str(), _p_dictmgr->installed_dict_names[index].c_str(), fontcolor.c_str()
			 );
	
	p_dict_htmllist->SetString(index, html);
}


/*!
 * DictPackPanel destructor
 */

DictPackPanel::~DictPackPanel()
{
////@begin DictPackPanel destruction
////@end DictPackPanel destruction
}

